﻿using Coolzon.UI.Core.Parts;
using CPPEI.Coolzon.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Xceed.Wpf.Toolkit.PropertyGrid;

namespace Coolzon.UI.Core
{
    public delegate void PropertyValueEditorValueChange(XPropertyValueEditor editor,
        XPropertyAttribute xProperty, 
        PropertyInfo propertyInfo, 
        object oldValue, 
        object newValue);

    /// <summary>
    /// 属性编辑器
    /// </summary>
    public abstract class XPropertyValueEditor : UserControl, Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor
    {
        public XPropertyAttribute XProperty { get; private set; }
        public PropertyInfo PropertyInfo { get; private set; }
        public Widget PartInstance { get; private set; }
        public object OriginalValue { get; private set; }

        protected object _currentValue;
        protected object _oldValue;

        public PropertyItem PropertyItem { get; set; }

        private bool _IsResolved = false;

        public object GetXPropertyValue()
        {
            return _currentValue;
        }

        /// <summary>
        /// 初始化编辑组件
        /// </summary>
        /// <param name="partInstance">组件实例</param>
        /// <param name="xProperty"></param>
        /// <param name="propertyInfo"></param>
        /// <param name="value">当前值</param>
        public void XInit(Widget partInstance, XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
            OriginalValue = value;
            PartInstance = partInstance;
            XProperty = xProperty;
            PropertyInfo = propertyInfo;

            if (value != null)
            {
                OnXInit(PartInstance, xProperty, propertyInfo, value);
            }
            _currentValue = value;
        }

        /// <summary>
        /// 初始化操作
        /// </summary>
        /// <param name="partInstance"></param>
        /// <param name="xProperty"></param>
        /// <param name="propertyInfo"></param>
        /// <param name="value"></param>
        protected abstract void OnXInit(Widget partInstance, XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value);
        protected abstract void OnXInit(PropertyItem propertyItem);

        protected abstract DependencyProperty GetPropertyValue();

        [Obsolete("使用XInit")]
        public void InitXPropertyValue(XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
            XProperty = xProperty;
            PropertyInfo = propertyInfo;

            if (value != null)
            {
                OnInitXPropertyValue(xProperty, propertyInfo, value);
            }
            _currentValue = value;
        }
        
        /// <summary>
        /// 属性值变更的事件
        /// </summary>
        public event PropertyValueEditorValueChange ValueChange;

        /// <summary>
        /// 初始化属性和属性值
        /// </summary>
        /// <param name="xProperty"></param>
        /// <param name="propertyInfo"></param>
        /// <param name="value"></param>
        protected virtual void OnInitXPropertyValue(XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
        }

        /// <summary>
        /// 更新属性值，会向外发送更新事件
        /// </summary>
        /// <param name="newValue"></param>
        //protected void UpdateXPropertyValue(object newValue)
        //{
        //    _oldValue = _currentValue;
        //    _currentValue = newValue;
        //    if (ValueChange != null && !object.Equals(_oldValue, _currentValue))
        //    {
        //        ValueChange(this, XProperty, PropertyInfo, _oldValue, _currentValue);
        //    }
        //}
        protected void UpdateXPropertyValue(object newValue)
        {
            //_oldValue = _currentValue;
            //_currentValue = newValue;
            //if (!object.Equals(_oldValue, _currentValue) && _IsResolved)
            //{
            //    PropertyItem.Editor = this;
            //    Designtime.EditorEventBus.Publish(new Designtime.EditorValueChangedEvent(PropertyItem, GetPropertyValue(), _currentValue));
            //}
        }

        public void UnDo(object oldValue)
        {
            _currentValue = oldValue;
            OnUnDo(oldValue);
        }

        protected virtual void OnUnDo(object oldValue)
        {

        }

        public FrameworkElement ResolveEditor(PropertyItem propertyItem)
        {
            PropertyItem = propertyItem;
            PartInstance = propertyItem.Instance as Widget;
            _currentValue = PropertyItem.Value;
            OnXInit(propertyItem);
            _IsResolved = true;
            return this;
        }
    }
}
